import webbrowser
import time
import subprocess
import sys
import os

def start_server():
    # 启动 Flask 服务
    server_process = subprocess.Popen([sys.executable, 'web/server.py'])
    
    # 等待服务启动
    time.sleep(2)
    
    # 打开默认浏览器
    webbrowser.open('http://localhost:5001')
    
    try:
        # 保持服务运行
        server_process.wait()
    except KeyboardInterrupt:
        # 处理 Ctrl+C
        print("\nShutting down server...")
        server_process.terminate()
        server_process.wait()

if __name__ == '__main__':
    # 确保从项目根目录运行
    project_root = os.path.dirname(os.path.abspath(__file__))
    os.chdir(project_root)
    
    print("Starting server...")
    start_server()
